﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class WeekEdit : System.Web.UI.Page
    {
        protected Weekly weekly;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string formNo = Request.QueryString["formNo"];
                if (formNo != null)
                {
                    weekly = ctx.Weeklies.Single(r => r.FormNo == formNo);
                    Employee employee = ctx.Employees.Single(r => r.Id == weekly.EmpId);
                    empName = employee.Name;
                    BeginDate.Value = weekly.BeginDate.ToString("yyyy-MM-dd");
                    EndDate.Value = weekly.EndDate.ToString("yyyy-MM-dd");
                    Summary.Value = weekly.Summary;
                    TxtPlan.Value = weekly.TxtPlan;
                    Suggestion.Value = weekly.Suggestion;
                    Inspiration.Value = weekly.Inspiration;
                    Improve.Value = weekly.Improve;
                }
                else
                {
                    string empId = Request.QueryString["empId"];
                    Employee employee = ctx.Employees.Single(r => r.Id == int.Parse(empId));
                    empName = employee.Name;
                    weekly = new Weekly();
                    weekly.CreatDate = DateTime.Now;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string formNo = Request.QueryString["formNo"];
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (!string.IsNullOrEmpty(formNo) && formNo != "undefined")
            {
                weekly = ctx.Weeklies.Single(r => r.FormNo == formNo);
                //删除之前的签核数据
                var list = from r in ctx.SignFlows where r.FormNo == formNo select r;
                ctx.SignFlows.DeleteAllOnSubmit(list);
            }
            else
            {
                weekly = new Weekly();
                string maxFormNo = ctx.Weeklies.Max(r => r.FormNo);
                formNo = "Form_Week_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Week_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                weekly.FormNo = formNo;
                weekly.CreatDate = DateTime.Now;
                weekly.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.Weeklies.InsertOnSubmit(weekly);
            }
            weekly.BeginDate = DateTime.Parse(BeginDate.Value);
            weekly.EndDate = DateTime.Parse(EndDate.Value);
            weekly.Summary = Summary.Value;
            weekly.TxtPlan = TxtPlan.Value;
            weekly.Suggestion = Suggestion.Value;
            weekly.Inspiration = Inspiration.Value;
            weekly.Improve = Improve.Value;
            weekly.StatusId = isSubmit;
            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                fs.SetSignFlow(weekly.FormNo, ebi);
            }
            Response.Write("<script>alert('修改成功');window.returnValue='" + weekly.FormNo + "';window.close();</script>");
        }
    }
}